const express = require('express');
const router = express.Router();

// 生成表单Schemas接口
router.post('/api/generate-form-schemas', (req, res) => {
  try {
    const { data, mode = 'vben' } = req.body;
    
    // 验证参数
    if (!data || !Array.isArray(data)) {
      return res.status(400).json({ error: '参数格式错误: data必须是数组' });
    }
    
    let schemasCode = '';
    
    if (mode === 'longnows') {
      // 生成 longnows 格式的 Schemas
      schemasCode = '[\n';
      data.forEach((item, index) => {
        schemasCode += '    {\n';
        schemasCode += `      ...useFormItemConfig('${item.label}', '${item.value}'),\n`;
        
        // 根据组件类型添加特定属性
        switch (item.componentType || 'Input') {
          case 'Input':
            schemasCode += '      ...inputCpmGetter(50, {\n';
            if (item.disabled === true) {
              schemasCode += `        disabled: true,\n`;
            }
            if (item.allowClear === true) {
              schemasCode += `        allowClear: true,\n`;
            }
            schemasCode += '      }),\n';
            break;
          case 'Select':
            schemasCode += `      ...selectCpmGetter([], {\n`;
            if (item.disabled === true) {
              schemasCode += `        disabled: true,\n`;
            }
            if (item.allowClear === true) {
              schemasCode += `        allowClear: true,\n`;
            }
            schemasCode += '      }),\n';
            break;
          case 'ParamSelect':
            schemasCode += `      ...pmSelectCpmGetter(\'${item.kind}', {\n`;
            if (item.disabled === true) {
              schemasCode += `        disabled: true,\n`;
            }
            if (item.allowClear === true) {
              schemasCode += `        allowClear: true,\n`;
            }
            schemasCode += '      }),\n';
            break;
          case 'DatePicker':
            schemasCode += '      ...dpCpmGetter(),\n';
            break;
        }
        
        schemasCode += '      required: false,\n';
        schemasCode += '    }';
        if (index < data.length - 1) {
          schemasCode += ',';
        }
        schemasCode += '\n';
      });
      schemasCode += '  ]';
    } else {
      // 生成 useForm Schemas 格式数据 (默认vben格式)
      schemasCode = '[\n';
      data.forEach((item, index) => {
        schemasCode += '  {\n';
        schemasCode += `    field: '${item.value}',\n`;
        schemasCode += `    label: '${item.label}',\n`;
        schemasCode += `    component: '${item.componentType || 'Input'}',\n`;
        schemasCode += '    required: false,\n';
        schemasCode += '    colProps: {\n';
        schemasCode += '      span: 24,\n';
        schemasCode += '    },\n';
        
        // 根据组件类型添加特定属性
        switch (item.componentType || 'Input') {
          case 'Input':
            schemasCode += '    componentProps: ({ formModel, schema, formActionType }) => {\n';
            schemasCode += '      return {\n';
            if (item.disabled === true) {
              schemasCode += `        disabled: true,\n`;
            }
            if (item.allowClear === true) {
              schemasCode += `        allowClear: true\n`;
            }
            schemasCode += '      }\n';
            schemasCode += '    },\n';
            break;
          case 'Select':
            schemasCode += '    componentProps: ({ formModel, schema, formActionType }) => {\n';
            schemasCode += '      return {\n';
            schemasCode += `        options: [],\n`;
            if (item.disabled === true) {
              schemasCode += `        disabled: true,\n`;
            }
            if (item.allowClear === true) {
              schemasCode += `        allowClear: true\n`;
            }
            schemasCode += '      }\n';
            schemasCode += '    },\n';
            break;
          case 'ParamSelect':
            schemasCode += '    componentProps: ({ formModel, schema, formActionType }) => {\n';
            schemasCode += '      return {\n';
            schemasCode += `        kind: '${item.kind}',\n`;
            if (item.disabled === true) {
              schemasCode += `        disabled: true,\n`;
            }
            if (item.allowClear === true) {
              schemasCode += `        allowClear: true\n`;
            }
            schemasCode += '      }\n';
            schemasCode += '    },\n';
            break;
          case 'DatePicker':
            schemasCode += '    componentProps: ({ formModel, schema, formActionType }) => {\n';
            schemasCode += '      return {\n';
            schemasCode += `        valueFormat: 'YYYY-MM-DD',\n`;
            if (item.disabled === true) {
              schemasCode += `        disabled: true,\n`;
            }
            if (item.allowClear === true) {
              schemasCode += `        allowClear: true\n`;
            }
            schemasCode += '      }\n';
            schemasCode += '    },\n';
            break;
        }
        
        schemasCode += '  }';
        if (index < data.length - 1) {
          schemasCode += ',';
        }
        schemasCode += '\n';
      });
      schemasCode += ']';
    }
    
    res.json({ code: schemasCode });
  } catch (error) {
    console.error('Generate form schemas error:', error);
    res.status(500).json({ error: '生成表单Schemas失败' });
  }
});

// 生成表格Columns接口
router.post('/api/generate-table-columns', (req, res) => {
  try {
    const { data, mode = 'vben' } = req.body;
    
    // 验证参数
    if (!data || !Array.isArray(data)) {
      return res.status(400).json({ error: '参数格式错误: data必须是数组' });
    }
    
    let columnsCode = '';
    
    if (mode === 'longnows') {
      // 生成 longnows 格式的 Columns
      columnsCode = '[\n';
      data.forEach((item, index) => {
        columnsCode += '    {\n';
        columnsCode += `      ...useTableItemConfig(t('${item.label}'), '${item.value}', 150, false, true),\n`;
        // 为ParamSelect类型添加customRender
        if (item.componentType === 'ParamSelect' && item.kind) {
          columnsCode += `      customRender: ({ text }) => {\n`;
          columnsCode += `        return text ? ParamManager.getInstance().getName('${item.kind}', text) : '';\n`;
          columnsCode += `      },\n`;
        }
        columnsCode += '    }';
        if (index < data.length - 1) {
          columnsCode += ',';
        }
        columnsCode += '\n';
      });
      columnsCode += '  ]';
    } else {
      // 生成 useTable Columns 格式数据 (默认vben格式)
      columnsCode = '[\n';
      data.forEach((item, index) => {
        columnsCode += '  {\n';
        columnsCode += `    title: '${item.label}',\n`;
        columnsCode += `    dataIndex: '${item.value}',\n`;
        columnsCode += `    key: '${item.value}',\n`;
        columnsCode += `    width: 200,\n`;
        columnsCode += `    sorter: false,\n`;
        columnsCode += `    showSorterTooltip: false,\n`;
        columnsCode += `    ellipsis: false,\n`;
        columnsCode += `    resizable: false,\n`;
        // 为ParamSelect类型添加customRender
        if (item.componentType === 'ParamSelect' && item.kind) {
          columnsCode += `    customRender: ({ text }) => {\n`;
          columnsCode += `      return text ? ParamManager.getInstance().getName('${item.kind}', text) : '';\n`;
          columnsCode += `    },\n`;
        }
        columnsCode += '  }';
        if (index < data.length - 1) {
          columnsCode += ',';
        }
        columnsCode += '\n';
      });
      columnsCode += ']';
    }
    
    res.json({ code: columnsCode });
  } catch (error) {
    console.error('Generate table columns error:', error);
    res.status(500).json({ error: '生成表格Columns失败' });
  }
});

module.exports = router;